# EA SPF DATA CPI Clean
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_easpf/","", collapse = NULL)

library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(tidyr)
library(robustHD)

###### CLEAN CPI ######
for (ii in 1999:2020) {
  for (jj in 1:4){
    tmp   = paste(indata,paste0(paste0(paste0(ii,"Q"),jj),".csv"),sep="")
    data_tmp = read.csv(tmp, skip = 1)
    
    data_tmp$id       = data_tmp$FCT_SOURCE
    data_tmp$target   = data_tmp$TARGET_PERIOD
    data_tmp$forecast = data_tmp$POINT
    
    data_tmp = subset(data_tmp, select = c(id,target,forecast))
    
    ind               = grep("^CORE",data_tmp$target)
    data_inf          = data_tmp[1:ind[1]-1,]
    
    data_inf          = spread(data_inf, target, forecast)
    data_inf          = data_inf[-c(1),]
    data_inf          = data_inf[c(1,4,6)]
    
    names(data_inf)[2] = "forecast1"
    names(data_inf)[3] = "forecast2"
    
    data_inf$year    = ii
    data_inf$quarter = jj
    
    data  = data_inf
    tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
    save(data, file = tmp)
  }
}


rm(list=ls())
for (ii in 1999:2020) {
  for (jj in 1:4){
    tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
    load(tmp)
    
    data_tmp = data
    
    if (ii == 1999 && jj == 1 ) {
      data_final  = data_tmp
    } else {
      data_final = merge(data_final,data_tmp, all = "TRUE")
    }
    
  }
}

data              = data_final
data$forecast1    = as.numeric(levels(data$forecast1))[as.integer(data$forecast1)]
data$forecast2    = as.numeric(levels(data$forecast2))[as.integer(data$forecast2)]
data$id           = as.numeric(data$id)

data = data[order(data$year,data$quarter),]

data$quarter_h1                    = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1                       = data$year + 1
data$year_h1[data$quarter == 1]    = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter_h1
data$year_h2    = data$year_h1+1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,forecast1) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,forecast1) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,forecast2) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,forecast2) )

data_clean       = merge(data_f1, data_f2, all = "TRUE")

data_inf        = read_excel("_easpf/data_realizations.xlsx", sheet = "Export")
data_inf        = subset(data_inf, select = c(year,quarter, rcpi_yoy,flpi) )

data_clean      = merge(data_clean, data_inf)


###### SAVE THE DATA CPI ########
data            = data_clean
data$qdate      = data$year + data$quarter/4-0.25
data$fc_err     = data$rcpi_yoy-data$forecast1
data$fc_rev     = data$forecast1-data$forecast2 
data$f1         = data$forecast1
data$f2         = data$forecast2
data$p_realz    = data$rcpi_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))

data            = data[data$year>="2000",]

save(data,file="ea_spf_cpi_ind.rda")
clear()


